﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eneta.Portal.Common.Definitions;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Utilities;
using Eneta.Portal.Common.Helpers;

namespace Eneta.Portal.Common.Dto
{
    public class LiteratureDto
    {
        public int ID { get; set; }
        public string ItemUrl { get; set; }
        public string AdditionalInfoURL { get; set; }
        public string LiteratureSmallImageUrl { get; set; }
        public string LiteratureImageUrl { get; set; }
        public string Summary { get; set; }
        public string ShortSummary { get; set; }
        public int AuthorId { get; set; }
        public string Author { get; set; }
        public string AuthorEmail { get; set; }
        public string LiteratureTags { get; set; }
        public string Title { get; set; }
        public int PosterProfileId { get; set; }
        public DateTime PostedDate { get; set; }
        public DateTime LastModified { get; set; }
        public List<KeywordDto> Keywords { get; set; }
        public string BookAuthors { get; set; }
        public string PublishingYear { get; set; }
        public string PageCount { get; set; }

        public string Isbn13
        {
            get;
            set;
        }

        public string Isbn10
        {
            get; set;
        }

        public SPModerationStatusType Status;
        public string StatusComment;

        public LiteratureDto(SPWeb web, SPListItem item)
        {
            ID = item.ID;
            this.Keywords = new List<KeywordDto>();
            var value = item["AdditionalInfoURL"];
            if (value != null)
            {
                var additionalInfoUrl = new SPFieldUrlValue(value.ToString());
                AdditionalInfoURL = additionalInfoUrl.Url;
            }
            else
            {
                AdditionalInfoURL = string.Empty;
            }
            LiteratureImageUrl = GetPictureUrl(item["FileLeafRef"].ToString(), web.Name);
            LiteratureSmallImageUrl = GetSmallPictureUrl(item["FileLeafRef"].ToString(), web.Name);
            Summary = item[SPFields.PublishingPageContent] as string;
            var user = new SPFieldUserValue(web, item[Constants.Fields.RealAuthor.Name] as string);
            if (user.User != null)
            {
                AuthorId = user.User.ID;
                Author = user.User.Name;
                AuthorEmail = user.User.Email;

                var profileRepository = new UserProfileRepository(web);
                var profile = profileRepository.GetUserProfileByProfileName(user.User.Name);
                if (profile != null)
                    PosterProfileId = profile.Id;
            }
            Title = item.Title;
            ItemUrl = web.ServerRelativeUrl + "/" + item.Url;
            PostedDate = (DateTime)item[SPBuiltInFieldId.Created];
            LastModified = (DateTime)item[SPFields.Modified];
            var keywordsValues = item[Constants.Fields.EnetaKeywords.Name] as SPFieldLookupValueCollection;
            if (keywordsValues != null)
            {
                foreach (var keywordValue in keywordsValues)
                {
                    this.Keywords.Add(new KeywordDto() { Id = keywordValue.LookupId, Title = keywordValue.LookupValue });
                }
            }

            if (item.ModerationInformation != null)
            {
                Status = item.ModerationInformation.Status;
                StatusComment = item.ModerationInformation.Comment;
            }

            
            int shortDescriptionLength = Summary.Length > 255 ? 255 : Summary.Length;
            this.ShortSummary = Summary.Substring(0, shortDescriptionLength) + " ";            
        }

        public LiteratureDto()
        {
            
        }

        private string GetPictureUrl(string pageName, string webname)
        {
            var pictureName = pageName.Replace(".aspx", ".png");
            var pictureUrl = "/" + Lists.SiteCollectionImages + "/" + webname + "/" + pictureName;//Lists.Books.Images + pictureName;
            return pictureUrl;
        }

        private string GetSmallPictureUrl(string pageName, string webname)
        {
            var pictureName = pageName.Replace(".aspx", "_small.png");
            var pictureUrl = "/" + Lists.SiteCollectionImages + "/" + webname + "/" + pictureName;// Lists.Books.Images + pictureName;
            return pictureUrl;
        }
    }
}
